<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Gregwar\Captcha\CaptchaBuilder;
class Login extends MY_Controller {
	public function __construct()
    {
        parent::__construct();
    }

	public function index()
	{
		$redirect = $this->input->get('redirect');
		if(!$redirect) $redirect = 'admin/main';
		if($this->session->name){
			redirect($redirect);
		}
		$this->load->view('/admin/login',array('redirect'=>site_url($redirect),'company_name'=>$this->getSetting('company_name'),'admin_title'=>$this->getSetting('admin_title')));
	}

	public function ajax_login(){
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$captcha = $this->input->post('captcha');
		if(empty($username)||empty($password)||empty($captcha)){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'请填写完整信息！')));
			return;
		}
		if(!$this->session->phrase){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'系统错误！')));
			return;
		}
		if($this->session->phrase != $captcha){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'验证码不正确！')));
			return;
		}
		$this->load->database();
		$user = $this->db->get_where('admin_users',array('username' => $username))->row();
		if(!$user){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'用户不存在！')));
			return;
		}
		if(password_verify($password , $user->password)){
			$this->session->name = $username;
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'ok','message'=>'登陆成功！')));
			return;
		}else{
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'密码错误！')));
			return;
		}
	}

	public function passwordChange(){
		$password = $this->input->post('password');
		$password_repeat = $this->input->post('password_repeat');
		$password_new = $this->input->post('password_new');
		if(empty($password)||empty($password_repeat)||empty($password_new)){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'请填写完整信息！')));
			return;
		}
		if($password == $password_new){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'新旧密码不能相同！')));
			return;
		}
		if($password_repeat != $password_new){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'重复密码不一致！')));
			return;
		}
		$user = $this->db->get_where('admin_users',array('username' => $this->session->name))->row();
		if(!password_verify($password , $user->password)){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'密码错误！')));
			return;
		}
		$hash = password_hash($password_new, PASSWORD_DEFAULT);
		$this->db->where('username', $this->session->name);
		$this->db->update('admin_users',array('password'=>$hash));
		$this->session->name = null;
		$this->output->set_content_type('application/json')
			 ->set_output(json_encode(array('statu' => 'ok','message'=>'修改成功！')));
		return;
	}

	public function nameChange(){
		$name = $this->input->post('name');
		if(empty($name)){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'请填写用户名！')));
			return;
		}
		if(strlen($name)<3){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'用户名过短！')));
				return;
		}
		if(strlen($name)>16){
			$this->output->set_content_type('application/json')
				 ->set_output(json_encode(array('statu' => 'failed','message'=>'用户名过长！')));
				return;
		}
		$this->db->where('username', $this->session->name);
		$this->db->update('admin_users',array('name'=>$name));
		$this->output->set_content_type('application/json')
			 ->set_output(json_encode(array('statu' => 'ok','message'=>'修改成功！')));
		return;
	}

	public function captcha(){
		$builder = new CaptchaBuilder;
		$builder->build();
		$this->session->phrase = $builder->getPhrase();
		header('Content-type: image/jpeg');
		$builder->output();
	}

	public function logout(){
		$this->session->name = null;
		redirect('admin/login');
	}
}
